
// pages/personpage/release/release.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title: '',
    imgList: [],
    textareaBValue: '',
    nickname: '',
    avatar: '',
    tags: [],
    ColorList: [{
      title: '闲逛',
      name: 'red',
      color: '#e54d42',
      state: false
    },
    {
      title: '旅游',
      name: 'red',
      color: '#e54d42',
      state: false
    },
    {
      title: '住宿',
      name: 'red',
      color: '#e54d42',
      state: false
    },
    {
      title: '推荐',
      name: 'red',
      color: '#e54d42',
      state: false
    }
  ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      nickname: app.globalData.userInfo.nickName,
      avatar: app.globalData.userInfo.avatarUrl
    })
    for(let i = 0; i < this.data.ColorList.length; i ++)
    {
      var color = "ColorList[" + i + "].state"
      this.setData({
        [color]: false
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 预览
  ViewImage(e) {
    wx.previewImage({
      urls: this.data.imgList,
      current: e.currentTarget.dataset.url
    });
  },
  // 选择图片
  ChooseImage() {
    const _this = this
    wx.chooseImage({
      count: 9, //默认9
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album'], //从相册选择
      success: (res) => {
        console.log(res)
        if (this.data.imgList.length != 0) {
          this.setData({
            imgList: this.data.imgList.concat(res.tempFilePaths)
          })
        } else {
          this.setData({
            imgList: res.tempFilePaths
          })
        }
        for (let i = 0; i < _this.data.imgList.length; i++) {
          wx.cloud.uploadFile({
            cloudPath: 'comments/' + new Date().getTime() + '.png',
            filePath: _this.data.imgList[i],
            success: res => {
              var file = 'fileIDs[' + i + ']'
              _this.setData({
                [file]: res.fileID
              })
              if (i === _this.data.imgList.length - 1) {
                wx.showToast({
                  title: '上传文件成功',
                })
              }
            }
          })
        }
      }
    });
  },

  DelImg(e) {
    wx.showModal({
      title: '亲爱的用户',
      content: '确定要删除这段回忆吗？',
      cancelText: '再看看',
      confirmText: '再见',
      success: res => {
        if (res.confirm) {
          this.data.imgList.splice(e.currentTarget.dataset.index, 1);
          this.setData({
            imgList: this.data.imgList
          })
        }
      }
    })
  },
  titleInput(e)
  {
    console.log(e)
    this.setData({
      title: e.detail.value
    })
  },
  textareaBInput(e) {
    this.setData({
      textareaBValue: e.detail.value
    })
  },

  releaseContent: function () {
    const _this = this
    var array_tag = new Array()
    for(let i = 0; i < _this.data.ColorList.length; i ++)
    {
        if(_this.data.ColorList[i].state == true)
        {
          array_tag.push(_this.data.ColorList[i].title)
        }
    }
    wx.cloud.callFunction({
      name: 'insertComments',
      data: {
        title: _this.data.title,
        openid: app.globalData.openid,
        nickname: app.globalData.userInfo.nickName,
        avatar: app.globalData.userInfo.avatarUrl,
        tags: array_tag,
        content: _this.data.textareaBValue,
        _createTime: new Date().getTime(),
        _updateTime: new Date().getTime()
      }
    }).then(res => {
      console.log(res)
      wx.showToast({
        title: '发布讨论成功',
      })
      wx.switchTab({
        url: '/pages/personpage/index',
      })
    })
  },
  
  selectTags: function(e)
  {
    console.log(e)
    var tag = e.currentTarget.dataset.tag
    var index = e.currentTarget.dataset.index
    var colorItem = 'ColorList[' + index + '].state'
    this.setData({
      [colorItem]: !tag.state
    })
  }
})